package com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.DO;

import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.dto.SentinelFlowRuleDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.io.Serializable;

/**
 * xx
 *
 * @author BugFree Jin
 * @version 1.0
 * @since 2022/3/2 15:46
 */
@Data
@Table(value = "sentinel_flow_rule")
public class SentinelFlowRuleDO extends BaseDO {

    private static final long serialVersionUID = -266770369319716200L;
    @Id
    private long id;

    @Column("flow_rule")
    private String flowRule;

    public SentinelFlowRuleDTO getSentinelFlowRuleDTO() {
        SentinelFlowRuleDTO sentinelFlowRuleDTO = new SentinelFlowRuleDTO();
        try {
            FlowRule fr = new ObjectMapper().readValue(this.flowRule, FlowRule.class);
            BeanUtils.copyProperties(fr, sentinelFlowRuleDTO);
            BeanUtils.copyProperties(this, sentinelFlowRuleDTO, "flowRule");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return sentinelFlowRuleDTO;
    }

    @Data
    public static class FlowRule implements Serializable
    {
        private Boolean clusterMode;
        private Integer controlBehavior;
        // todo: clusterConfig 为 json object
        private String clusterConfig;
        private Double count;
        private Integer grade;
        private Integer maxQueueingTimeMs;
        private String refResource;
        private Integer strategy;
        private Integer warmUpPeriodSec;
        private String limitApp;
        private String resource;
        private static final long serialVersionUID = -7332464226236336488L;
    }

}
